package com.chainyoung.component_tmperson.ui.mvp.model;

import com.chainyoung.common.appcommon.db.DbConstant;
import com.chainyoung.common.appcommon.entity.HdWalletCache;
import com.chainyoung.common.appcommon.entity.HdWalletTokenCache;
import com.chainyoung.common.appcommon.hdwallet.EtherscanService;
import com.chainyoung.common.appcommon.hdwallet.HdWalletDAO;
import com.chainyoung.common.appcommon.hdwallet.HdWalletTokenDAO;
import com.chainyoung.common.appcommon.hdwallet.TokenBalance;
import com.chainyoung.common.base.BaseResponse;
import com.chainyoung.common.integration.IRepositoryManager;
import com.chainyoung.common.mvp.BaseModel;
import com.chainyoung.common.utils.RxUtils;
import com.chainyoung.component_tmperson.api.PersonService;
import com.chainyoung.component_tmperson.entity.PreRepayment;
import com.chainyoung.component_tmperson.ui.mvp.contract.HdWalletContract;
import com.chainyoung.component_tmperson.ui.mvp.contract.HdWalletTokenListContract;

import org.json.JSONObject;

import java.util.List;

import javax.inject.Inject;

import io.reactivex.Flowable;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Function;
import okhttp3.ResponseBody;

public class HdWalletTokenListModel extends BaseModel implements HdWalletTokenListContract.Model {

    private HdWalletTokenDAO DAO = new HdWalletTokenDAO(mRepositoryManager.obtainRealmConfig(DbConstant.DB_NAME));

    @Inject
    HdWalletTokenListModel(IRepositoryManager repositoryManager) {
        super(repositoryManager);

    }


    @Override
    public Flowable<String> getAllTokenTransfers(String addr) {
        return mRepositoryManager.obtainRetrofitService(EtherscanService.class)
                .getAllTokenTransfers(addr)
                .map(new Function<ResponseBody, String>() {
                    @Override
                    public String apply(@NonNull ResponseBody body) throws Exception {
                        return new JSONObject(body.string()).getString("result");
                    }
                });
    }

    @Override
    public Flowable<TokenBalance> getTokenBalance(final String contractAddr, String addr) {
        return mRepositoryManager.obtainRetrofitService(EtherscanService.class)
                .getTokenBalance(contractAddr, addr)
                .map(new Function<ResponseBody, TokenBalance>() {
                    @Override
                    public TokenBalance apply(@NonNull ResponseBody body) throws Exception {
                        return new TokenBalance(contractAddr,new JSONObject(body.string()).getString("result"));
                    }
                });
    }


    @Override
    public Flowable<List<HdWalletTokenCache>> getAllHdWalletTokenCaches(String address) {
        return DAO.getHdWalletTokenCaches(address);
    }


    @Override
    public Flowable<HdWalletTokenCache> addHdWalletTokenCache(HdWalletTokenCache item) {
        return DAO.addHdWalletTokenCache(item);
    }

    @Override
    public Flowable<String> getTokenNames() {
        return mRepositoryManager.obtainRetrofitService(PersonService.class)
                .getTokenNames()
                .map(new Function<ResponseBody, String>() {
                    @Override
                    public String apply(@NonNull ResponseBody body) throws Exception {
                        return new JSONObject(body.string()).getString("data");
                    }
                });
    }


}



